﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using HPT.Utilities;

namespace HPT
{
    public class PresenterNews
    {
        private IViewNews view;

        public PresenterNews(IViewNews view)
        {
            // TODO: Complete member initialization
            this.view = view;
        }
        //Get all news
        public void GetAllNewsByCategory()
        {
            List<NewsModal> allNews = new List<NewsModal>();
            SPList listNews = OWeb.GetList(Constants.List.News);
            SPQuery query = new SPQuery();
            query.RowLimit = Convert.ToUInt32(view.RowLimit);
            query.Query = String.Format("<Where><And><Or><IsNull><FieldRef Name='Publish' /></IsNull><Eq><FieldRef Name='Publish' /><Value Type='Boolean'>1</Value></Eq></Or> <Eq><FieldRef Name='Category' /><Value Type='Choice'>{0}</Value></Eq></And></Where><OrderBy><FieldRef Name='DisplayOrder' Ascending='False' /></OrderBy>", view.NewsCategory);
            SPListItemCollection itemNews = null;
            itemNews = listNews.GetItems(query);
            foreach (SPListItem item in itemNews)
                allNews.Add(new NewsModal(item));
            view.AllNews = allNews;
        }


        public void GetAllHotNews()
        {
            List<NewsModal> allNews = new List<NewsModal>();
            SPList listNews = OWeb.GetList(Constants.List.News);
            SPQuery query = new SPQuery();
            query.Query = "<Where><Or><IsNull><FieldRef Name='Publish' /></IsNull><Eq><FieldRef Name='Publish' /><Value Type='Boolean'>1</Value></Eq></Or></Where><OrderBy><FieldRef Name='DisplayOrder' Ascending='False' /></OrderBy>";
            query.RowLimit = Convert.ToUInt32(view.RowLimit);
            SPListItemCollection itemNews = null;
            itemNews = listNews.GetItems(query);
            foreach (SPListItem item in itemNews)
                allNews.Add(new NewsModal(item));
            view.AllNews = allNews;
        }



        public List<NewsModal> GetBestReadNews()
        {
            List<NewsModal> allNews = new List<NewsModal>();
            SPList listNews = OWeb.GetList(Constants.List.News);
            SPQuery query = new SPQuery();
            query.Query = "<Where><Or><IsNull><FieldRef Name='Publish' /></IsNull><Eq><FieldRef Name='Publish' /><Value Type='Boolean'>1</Value></Eq></Or></Where><OrderBy><FieldRef Name='HitCounter' Ascending='False' /></OrderBy>";
            query.RowLimit = Convert.ToUInt32(view.RowLimit);
            SPListItemCollection itemNews = null;
            itemNews = listNews.GetItems(query);
            foreach (SPListItem item in itemNews)
                allNews.Add(new NewsModal(item));
            return allNews;
        }
    }
}
